package action.account;

import java.util.List;

import model.Account;
import model.AccountQuestion;
import model.Page;

import DAO.AccountDAO;
import DAO.DAOImple.AccountImple;

import com.opensymphony.xwork2.ActionSupport;

public class QuestionAction extends ActionSupport {
	private Account account;
	private AccountQuestion question;
	private List<AccountQuestion> questions;
	
	private Page page;
	
	public String execute() {

		AccountDAO accountDao = new AccountImple();
		account = accountDao.getAccount(account.getId());
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_AccountQuestion where accountId = "+ account.getId() +" and state != 2",page.getCurrentPage(),20);  //获得分页对象
		questions = accountDao.getQuestionList(account.getId(),page);
		return SUCCESS;
	}

	public String newQuestion() {
		AccountDAO accountDao = new AccountImple();
		if(!(question.getQuestionContent().trim().equals(""))){
			int newResult = accountDao.newQuestion(question);
			if(newResult == 0) {
				//System.out.println("失败");
			}
		}
		account = accountDao.getAccount(question.getAccount().getId());
		if(page == null) {
			page = new Page();
			page.setCurrentPage(1);
		}
		page = page.getPage("T_AccountQuestion where accountId = "+ account.getId() +" and state != 2",page.getCurrentPage(),20);  //获得分页对象
		questions = accountDao.getQuestionList(account.getId(),page);
		
		return SUCCESS;
		
	}
	
	//回答或修改问题答案
	public String replyOrAnswerQuestion() {
		AccountDAO accountDao = new AccountImple();
		if(!(question.getSolution().trim().equals(""))){
			int result = accountDao.replyOrAnswerQuestion(question);
			if(result == 0) {
				//System.out.println("失败");
			}else {
				account = accountDao.getAccount(question.getAccount().getId());
				
				if(page == null) {
					page = new Page();
					page.setCurrentPage(1);
				}
				page = page.getPage("T_AccountQuestion where accountId = "+ account.getId() +" and state != 2",page.getCurrentPage(),20);  //获得分页对象
				questions = accountDao.getQuestionList(account.getId(),page);
			}
		}else {
			account = accountDao.getAccount(question.getAccount().getId());

			if(page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_AccountQuestion where accountId = "+ account.getId() +" and state != 2",page.getCurrentPage(),20);  //获得分页对象
			questions = accountDao.getQuestionList(account.getId(),page);
		}
		return SUCCESS;
	}
	
	//删除问题
	public String deleteAccountQ() {
		AccountDAO accountDao = new AccountImple();
		int result = accountDao.deleteAccountQ(question);
		if(result == 0) {
			//System.out.println("失败");
		}else {
			account = accountDao.getAccount(question.getAccount().getId());

			if(page == null) {
				page = new Page();
				page.setCurrentPage(1);
			}
			page = page.getPage("T_AccountQuestion where accountId = "+ account.getId() +" and state != 2",page.getCurrentPage(),20);  //获得分页对象
			questions = accountDao.getQuestionList(account.getId(),page);
		}
		return SUCCESS;
	}
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public AccountQuestion getQuestion() {
		return question;
	}

	public void setQuestion(AccountQuestion question) {
		this.question = question;
	}

	public List<AccountQuestion> getQuestions() {
		return questions;
	}

	public void setQuestions(List<AccountQuestion> questions) {
		this.questions = questions;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
}
